package org.j5108.dao;

import java.sql.Timestamp;
import java.util.List;

import org.hibernate.LockOptions;
import org.hibernate.Query;
import org.hibernate.criterion.Example;
import org.j5108.entity.Traffic;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * A data access object (DAO) providing persistence and search support for
 * Traffic entities. Transaction control of the save(), update() and delete()
 * operations can directly support Spring container-managed transactions or they
 * can be augmented to handle user-managed Spring transactions. Each of these
 * methods provides additional information for how to configure it for the
 * desired type of transaction control.
 * 
 * @see org.j5108.entity.Traffic
 * @author MyEclipse Persistence Tools
 */
public class TrafficDAO extends BaseHibernateDAO {
	private static final Logger log = LoggerFactory.getLogger(TrafficDAO.class);
	// property constants
	public static final String SOURCE_URL = "sourceUrl";
	public static final String TARGET_URL = "targetUrl";
	public static final String IP = "ip";
	public static final String AREA = "area";

	public void save(Traffic transientInstance) {
		log.debug("saving Traffic instance");
		try {
			getSession().save(transientInstance);
			log.debug("save successful");
		} catch (RuntimeException re) {
			log.error("save failed", re);
			throw re;
		}
	}

	public void delete(Traffic persistentInstance) {
		log.debug("deleting Traffic instance");
		try {
			getSession().delete(persistentInstance);
			log.debug("delete successful");
		} catch (RuntimeException re) {
			log.error("delete failed", re);
			throw re;
		}
	}

	public Traffic findById(java.lang.Integer id) {
		log.debug("getting Traffic instance with id: " + id);
		try {
			Traffic instance = (Traffic) getSession().get(
					"org.j5108.entity.Traffic", id);
			return instance;
		} catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
	}

	public List findByExample(Traffic instance) {
		log.debug("finding Traffic instance by example");
		try {
			List results = getSession()
					.createCriteria("org.j5108.entity.Traffic")
					.add(Example.create(instance)).list();
			log.debug("find by example successful, result size: "
					+ results.size());
			return results;
		} catch (RuntimeException re) {
			log.error("find by example failed", re);
			throw re;
		}
	}

	public List findByProperty(String propertyName, Object value) {
		log.debug("finding Traffic instance with property: " + propertyName
				+ ", value: " + value);
		try {
			String queryString = "from Traffic as model where model."
					+ propertyName + "= ?";
			Query queryObject = getSession().createQuery(queryString);
			queryObject.setParameter(0, value);
			return queryObject.list();
		} catch (RuntimeException re) {
			log.error("find by property name failed", re);
			throw re;
		}
	}

	public List findBySourceUrl(Object sourceUrl) {
		return findByProperty(SOURCE_URL, sourceUrl);
	}

	public List findByTargetUrl(Object targetUrl) {
		return findByProperty(TARGET_URL, targetUrl);
	}

	public List findByIp(Object ip) {
		return findByProperty(IP, ip);
	}

	public List findByArea(Object area) {
		return findByProperty(AREA, area);
	}

	public List findAll() {
		log.debug("finding all Traffic instances");
		try {
			String queryString = "from Traffic";
			Query queryObject = getSession().createQuery(queryString);
			return queryObject.list();
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}

	public Traffic merge(Traffic detachedInstance) {
		log.debug("merging Traffic instance");
		try {
			Traffic result = (Traffic) getSession().merge(detachedInstance);
			log.debug("merge successful");
			return result;
		} catch (RuntimeException re) {
			log.error("merge failed", re);
			throw re;
		}
	}

	public void attachDirty(Traffic instance) {
		log.debug("attaching dirty Traffic instance");
		try {
			getSession().saveOrUpdate(instance);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public void attachClean(Traffic instance) {
		log.debug("attaching clean Traffic instance");
		try {
			getSession().buildLockRequest(LockOptions.NONE).lock(instance);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}
}